package com.len.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author: xudeli
 * @Description: 字符串随机排序
 * @CreateDate: 2018/3/25 23:21
 **/
public class RandomStrUtils {
    //假设一个字符串是abc 要求输出，abc,acb,bac,bca,cab,cba
    public static List<String> Permutation(String str) {
        ArrayList<String> list = new ArrayList<String>();
        char[] ch = str.toCharArray();
        Permu(ch, 0, list);
        System.out.println("-----------");
        System.out.println(list);
        System.out.println("-----------");
        Collections.sort(list);
        return list;
    }

    public static void Permu(char[] str, int i, ArrayList<String> list) {
        if (str == null) {
            return;
        }
        if (i == str.length - 1) {
            if (list.contains(String.valueOf(str))) {
                return;
            }
            list.add(String.valueOf(str));
        } else {
            boolean num = true;
            for (int j = i; j < str.length; j++) {
                char temp = str[j];
                str[j] = str[i];
                str[i] = temp;
                Permu(str, i + 1, list);
                temp = str[j];
                str[j] = str[i];
                str[i] = temp;
            }
        }
    }
}
